﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace eNew.ConsoleApps.ImportTxtToSQL.BusinessLogic
{
    using DAL;
    using BusinessEntities;
    class LogicFile
    {


        public static void readFile()
        {
            int counter = 0;

            string line;
            List<string> mylist = new List<string>();
            List<string> outlist = new List<string>();
            System.IO.StreamReader file2 =
           new System.IO.StreamReader(@"c:\test\afs.txt");

            System.IO.StreamReader file =
            new System.IO.StreamReader(@"c:\test\afs.txt");

            //System.Console.WriteLine(findEdate(file).ToString());      

            while (!file.EndOfStream)
            {
                line = file.ReadLine().Trim();

                if (!string.IsNullOrEmpty(line))
                {
                    mylist.Add(line.Trim());

                }
                counter++;

            }

            mylist.Sort();


            string ed = findEdate(file2).ToString();

            foreach (string s in mylist)
            {
                if (s.Substring(0, 5).ToString() == "TOTAL" || s.Substring(0, 3).ToString().ToUpper() == "###" || s.Substring(0, 3).ToString().ToUpper() == "FOR" || s.Substring(0, 3).ToString().ToUpper() == "JOB" || s.Substring(0, 3).ToString().ToUpper() == "PGM" || s.Substring(0, 3).ToString().ToUpper() == "AGY")
                {

                    mylist.Skip(1);

                }
                else
                {
                    string ln = ed + " " + s;
                     outlist.Add(ln);


                }

                string extract = @"c:\test\extract.txt";
                if (File.Exists(extract))
                {
                    File.WriteAllLines(extract, outlist);

                }

               
            }
            System.Console.WriteLine("There were {0} lines.", counter);

            //Imported/Extracted Data To Database
            ImportedToDB(outlist);
                    

            System.Console.ReadLine();
        }
        public static void ImportedToDB(List<string> list)
        {
            StreamReader rdrDate = new StreamReader(file);
            string edate = findEdate(rdrDate);
            try
            {
                foreach (string items in list)
                {
                    int len = items.Length;

                    Console.WriteLine(items.Substring(0, 10).ToString());
                    Console.WriteLine(items.Substring(11, 11).ToString());
                    Console.WriteLine(items.Substring(24, 21).ToString());
                    Console.WriteLine(items.Substring(46, 10).ToString());
                    Console.WriteLine(items.Substring(57, 14).ToString()); //Check with Brian MaxLength of each column account no#
                    Console.WriteLine(items.Substring(72, 12).ToString().Trim());

                    Console.WriteLine(Double.Parse(items.Substring(85).ToString()));

                    ImportData rec = new ImportData(items);
                    SqlAccessLayer.InsertImportData(rec);






                    //Console.WriteLine(items);
                    //SQLAccessLayer.InsertImportData(items, edate);

                }



            }
            catch (Exception ex)
            {
                string err = string.Format(ex.ToString() + "\n" + ex.StackTrace.ToString());
                Console.WriteLine(err);
                // log.Info(string.Format(ex.ToString() + "\n" + ex.StackTrace.ToString()));
            }
        }
        private static string findEdate(StreamReader file)
        {
            List<string> removed = new List<string>();
           
             while (!file.EndOfStream)
            {
                string line = file.ReadLine().Trim();
                if (!string.IsNullOrEmpty(line))
                {
                    removed.Add(line.Trim());

                }
                

            }
            
            foreach (string s in removed)
            {
                //FOR THE PERIOD ENDING 06/25/2015
                if (s.Substring(0, 3).ToString().ToUpper() == "FOR")
                {
                    int len = s.Length;
                   
                    return s.Substring(22);

                   
                }

            }

            return string.Empty ;
        }
    }




        
}
    